﻿using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using Dcms.Config;
using System.Text;
using System.Data;
using System.IO;
using Dcms.Common;
using System.Configuration;
/// <summary>
/// WebInfoMag 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebInfoMag : System.Web.Services.WebService
{

    public WebInfoMag()
    {

        //如果使用设计的组件，请取消注释以下行 
        //InitializeComponent(); 
    }

    protected DataTable GetAllTemplateList()
    {
        DirectoryInfo dirinfo = new DirectoryInfo(Utils.GetMapPath(ConfigurationManager.AppSettings["TempLatePath"]));
        DataTable dt = new DataTable();
        dt.Columns.Add("styleName", typeof(string));
        dt.Columns.Add("stylePage", typeof(string));
        foreach (DirectoryInfo dir in dirinfo.GetDirectories())
        {
            DataRow dr = dt.NewRow();
            dr["styleName"] = dir.Name;
            dr["stylePage"] = "aspx/" + dir.Name + "/";
            dt.Rows.Add(dr);
        }
        return dt;
    }
    protected bool saveXml(string xmlContent)
    {
        try
        {
            StreamWriter sw = new StreamWriter(Server.MapPath("~/SystemConfig/url.config"), false, System.Text.Encoding.UTF8);
            sw.Write(xmlContent);
            sw.Close();
            return true;
        }
        catch
        {
            return false;
        }
    }

    [WebMethod]
    public bool SetWebInfo(string webName, string keyWord)
    {
        try
        {
            WebSiteConfigInfo etWebSiteConfigInfo = WebSiteConfigs.Deserialize("~/SystemConfig/WebSiteInfo.config");
            etWebSiteConfigInfo.SiteName = webName;
            etWebSiteConfigInfo.Keyword = keyWord;
            StringBuilder xmlContent = new StringBuilder();
            xmlContent.Append("<?xml version=\"1.0\" standalone=\"yes\"?>\n<urls>");
            DataTable dt = GetAllTemplateList();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["styleName"].ToString().Trim() == etWebSiteConfigInfo.defaultStyle)
                {
                    xmlContent.Append("\n<rewrite name=\"" + dt.Rows[i]["styleName"] + "\" path=\"/\" value=\"-1\"  page=\"" + dt.Rows[i]["stylePage"] + "\" />");
                }
                else
                {
                    xmlContent.Append("\n<rewrite name=\"" + dt.Rows[i]["styleName"] + "\" path=\"" + dt.Rows[i]["styleName"] + "/\" value=\"0\"  page=\"" + dt.Rows[i]["stylePage"] + "\" />");
                }
            }
            xmlContent.Append("\n</urls>");

            return WebSiteConfigs.Serialiaze(etWebSiteConfigInfo, "~/SystemConfig/WebSiteInfo.config") && saveXml(xmlContent.ToString());
        }
        catch (Exception e)
        {
            return false;
        }

    }
}


